package testJdk.commonUse.string;

import cn.hutool.core.util.ArrayUtil;
import org.junit.Test;

import java.util.Arrays;
import java.util.StringJoiner;

/**
 * @version 1.0.0
 * Created by cyd on 2023-07-05 12:13:10
 * @ClassName : testJdk.commonUse.string.TestSpitAndJoiner
 * @Description : 类描述
 */
public class TestSpitAndJoiner {

    /**
     * 字符串分割和合并
     */
    @Test
    public void test() {
        String str = "abc-def-ghi";
        String[] split = str.split("-");
        System.out.println(Arrays.toString(split));

        // 直接拼接
        StringJoiner sj = new StringJoiner(" & ");
        for (String s : split) {
            sj.add(s);
        }
        System.out.println(sj.toString());

        // 加前后缀
        StringJoiner sj02 = new StringJoiner(" & ", "!", "?");
        for (String s : split) {
            sj02.add(s);
        }
        System.out.println(sj02.toString());
    }

    /**
     * ArrayUtil来拼接字符串数组
     */
    @Test
    public void test01() {
        String str = "abc-def-ghi";
        String[] split = str.split("-");
        System.out.println(Arrays.toString(split));
        System.out.println(ArrayUtil.join(split, ","));
        System.out.println(ArrayUtil.join(split, ",", "!-", "-!"));
    }


    /**
     * 复杂分割
     * 按照单词进行分割
     */
    @Test
    public void test02() {
        String str = "1.05.1-Code";
        // [^a-zA-Z] 来匹配不是字母的字符
        // [^a-zA-Z0-9] 来匹配不是字母和数字的字符
        String[] split = str.split("[^a-zA-Z0-9]");
        System.out.println(Arrays.toString(split));
        String major = "";
        String minor = "";
        String patch = "";
        String mile = "";
        if (split.length > 1) {
            major = split[0];
            minor = split[1];
        }
        if (split.length > 2) {
            patch = split[2];
        }
        if (split.length > 3) {
            mile = split[3];
        }

        System.out.println("major:" + Integer.valueOf(major));
        System.out.println("minor:" + Integer.valueOf(minor));
        System.out.println("patch:" + Integer.valueOf(patch));
        System.out.println("mile:" + mile);
    }

    /**
     * 复杂分割
     * 版本号分割
     */
    @Test
    public void test03() {
        String version = "1.05.1-Code";
        String major = "";
        String minor = "";
        String patch = "";
        String mile = "";
        String[] versionStrArr = version.split("-");
        if (versionStrArr.length > 1) mile = versionStrArr[1];
        String major_minor_patch = versionStrArr[0];

        String[] split = major_minor_patch.split("\\.");
        if (split.length == 3) patch = split[2];
        major = split[0];
        minor = split[1];

        System.out.println("major:" + Integer.valueOf(major));
        System.out.println("minor:" + Integer.valueOf(minor));
        System.out.println("patch:" + Integer.valueOf(patch));
        System.out.println("mile:" + mile);

    }

    /**
     * "/"的分割
     */
    @Test
    public void test04() {
        String productName = "海曼_HM-608/HM-608/深圳市海曼科技股份有限公司";
        String[] split = productName.split("/");
        System.out.println(Arrays.toString(split));
    }

}
